<?PHP
 require_once('inc/int.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <title>DMDirc :: <?PHP echo t('menu_nightly'); ?></title>
  <link rel="stylesheet" type="text/css" href="res/style.css">
  <link rel="icon" type="image/png" href="res/favicon.png">
 </head>
 <body>
  <div id="container">
   <img id="logo" src="res/logo.png" alt="DMDirc logo">
   <h1>DMDirc</h1>
<?PHP define('PAGE', 'releases'); define('SUBPAGE', 'nightly'); require_once('inc/menu.php'); ?>
   <div id="content">

    <h2><?PHP echo t('menu_nightly'); ?></h2>

    <p><?PHP echo t('nightly_blurb'); ?></p>

    <table class="nightlies">
     <tr>
      <th>Date</th>
      <th><img src="res/linux.png" alt="Linux">Linux</th>
      <th><img src="res/windows.png" alt="Windows">Windows</th>
      <th><img src="res/osx.png" alt="OS X">OS X</th>
      <th><img src="res/java.png" alt="Java / Other OS">Java / Other</th>
     </tr>
<?PHP

 if (($pos = strpos($_SERVER['REQUEST_URI'], '-')) !== false) {
  $month = substr($_SERVER['REQUEST_URI'], $pos + 1);
 }

 if (isset($month) && ctype_alnum($month) && is_dir('nightly/old/' . $month)) {
  define('DIR', 'nightly/old/' . $month . '/');
 } else {
  define('DIR', 'nightly/');
 }

 $files = glob(DIR . '*.*');
 $nightlies = array();

 foreach ($files as $file) {
  $file = substr($file, strlen(DIR)); // Stip the nightly/

  if (preg_match('#^DMDirc_([0-9]+)_.*?\.jar$#', $file, $m)) {
   $nightlies[(int) $m[1]]['java'] = $file;
  } else if (preg_match('#^DMDirc-Setup-([0-9]+)_.*?\.(exe|run)$#', $file, $m)) {
   $nightlies[(int) $m[1]][$m[2] == 'exe' ? 'windows' : 'linux'] = $file;
  } else if (preg_match('#^DMDirc-([0-9]+)_.*?\.dmg$#', $file, $m)) {
   $nightlies[(int) $m[1]]['osx'] = $file;
  }
 }

 krsort($nightlies);

 foreach ($nightlies as $date => $files) {
  echo '<tr><td>', date('d F Y', strtotime($date)), '</td><td>';

  foreach (array('linux', 'windows', 'osx', 'java') as $os) {
   if ($os != 'linux') { echo '</td><td>'; }
 
   if (isset($files[$os])) {
    echo '<a href="/', DIR, $files[$os], '">', substr($files[$os], -4), '</a>';
    echo ' (', round(filesize(DIR . $files[$os])/1024/1024,1), ' MiB)';
   } else {
    echo 'Not available';
   }
  }

 }

?>
    </table>
    <hr>
    <p><a href="nightlies">Current</a>
    <?PHP 
     $dirs = glob('nightly/old/*', GLOB_ONLYDIR);
     function stupidsort($a, $b) {
      $a = preg_replace('#^.*/(.*?)$#', '\1', $a);
      $b = preg_replace('#^.*/(.*?)$#', '\1', $b);
      return strnatcmp(strtotime(substr($b, 0, -2) . ' ' . substr($b, -2)),
                       strtotime(substr($a, 0, -2) . ' ' . substr($a, -2)));
     }
     
     usort($dirs, 'stupidsort');

     foreach ($dirs as $old) {
     echo '| <a href="nightlies-', ($month = preg_replace('#^.*/(.*?)$#', '\1', $old));
     echo '">' , ucfirst(substr($month, 0, -2)), ' 20', substr($month, -2), '</a> ';
    } ?>
    </p>
   </div>
   <div id="footer">
   </div>
  </div>
 </body>
</html>
